package com.apes.fn.scm.retailOrder.service.tc.tcOutbox;

import com.apes.fn.scm.retailOrder.service.tc.tcOutbox.TcOutbox;
import com.apes.framework.jpa.repository.CustomRepository;
import org.hibernate.validator.constraints.URL;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

public interface TcOutboxRepository extends CustomRepository<TcOutbox, Integer> {

    List findByStateAndAggregateType(Integer state,String aggregateType);

//    @Query("SELECT T FROM TcOutbox T WHERE T.state not in (:state) and t.outboxDate <= :date")
//    Page findByStateNotIn(@Param("state")Integer state, @Param("date") Date date, Pageable pageable);

    TcOutbox findByAggregateId(String aggregateId);
    List findByAggregateIdAndAggregateType(String aggregateId,String aggregateType);

    @Query(value = "SELECT * FROM tc_outbox T WHERE T.state = :state and t.outbox_date <= :date",nativeQuery=true)
    Page<TcOutbox> findByStateAndOutboxDateNotIn(@Param("state")Integer state, @Param("date") Date date, Pageable pageable);

    @Query("SELECT T FROM TcOutbox T WHERE T.aggregateId = :id AND T.error LIKE '%跨单%'")
    TcOutbox getTcMessage(@Param("id") String id);

    @Modifying
    @Query(value = "INSERT INTO TC_KDTC (ID, TJRQ, FN_INNER_PICK_DEL_UP_CAR_ID, PLATE_LICENSE," +
            " COMPANY_ID, DEPT_ID, BILL_MAN, TC_AMT,SOURCE,CREATE_DATE,BILLING_TYPE,COMPLETION_METHOD,CHILD_NODE_NAME) " +
            "VALUES (IR_TC_KDTC.NEXTVAL,SYSDATE,:orderId,:plateLicense,:companyId,:deptId,:billMan,:tcAmt," +
            ":source,SYSDATE,:billingType,:completionMethod,:childNodeName)",nativeQuery = true)
    void insertKDTC(@Param("orderId") String orderId, @Param("plateLicense") String plateLicense, @Param("companyId") String companyId,
              @Param("deptId") String deptId,@Param("billMan") String billMan,@Param("tcAmt") Double tcAmt,@Param("source") String source,
        @Param("billingType") String billingType,@Param("completionMethod") String completionMethod,@Param("childNodeName") String childNodeName);

    @Query(value = "SELECT A.* FROM TC_KDTC A WHERE A.FN_INNER_PICK_DEL_UP_CAR_ID = :orderId",nativeQuery = true)
    Map findByOrderId(@Param("orderId") String orderId);
}

